package testes_unitarios;

import static org.junit.Assert.*;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.*;

import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.ItemPedido;
import model.Pizza;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;

import controller.RemovePizzaServlet;
import dao.PizzaDAO;

@RunWith(MockitoJUnitRunner.class)
public class TestRemovePizzaServlet {
	
	RemovePizzaServlet servlet = new RemovePizzaServlet();
	
	@SuppressWarnings("unchecked")
	@Test
	public void serviceTest() throws ServletException, IOException, SQLException {
		
		HttpServletRequest request = mock(HttpServletRequest.class);
		HttpServletResponse response = mock(HttpServletResponse.class);
		HttpSession sessao = mock(HttpSession.class);
		RequestDispatcher rq = mock(RequestDispatcher.class);
		
		when(request.getSession(true)).thenReturn(sessao);
		when(request.getRequestDispatcher(anyString())).thenReturn(rq);
		doNothing().doThrow(new RuntimeException()).when(rq).forward(request, response);
		
				PizzaDAO pizzaDAO = new PizzaDAO();
				Pizza pizza  = pizzaDAO.recuperarPizza("Atum");
				Pizza pizza2  = pizzaDAO.recuperarPizza("Calabresa");
				
				ItemPedido item = new ItemPedido(pizza, 1);
				ItemPedido item2 = new ItemPedido(pizza2, 1);
				
				HashMap<String, ItemPedido> itens = new HashMap<>();
				
				itens.put(pizza.getNome(), item);
				itens.put(pizza2.getNome(), item2);
		
		when(request.getParameter("nomePizzaRemover")).thenReturn("Calabresa");
		when(sessao.getAttribute("itens")).thenReturn(itens);
		
		HashMap<String, ItemPedido> itens2 = (HashMap<String, ItemPedido>) sessao.getAttribute("itens");
		
		servlet.service(request, response);
		
		assertEquals(false, itens2.containsKey("Calabresa"));
		
		sessao.invalidate();
		
	}
	
}
